
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:permission_handler/permission_handler.dart';

import '../../user_data.dart';
import '../style/app_style.dart';
import '../utils/storage_util.dart';


class PermissionWidget extends StatefulWidget {
  // const PermissionWidget({super.key});
  const PermissionWidget({super.key,this.desc,this.permission});
  final String? desc;
  final Permission? permission;

  @override
  State<PermissionWidget> createState() => _PermissionWidgetState();
}

class _PermissionWidgetState extends State<PermissionWidget> {

  @override
  void initState() {
    Future.delayed(const Duration(milliseconds: 100),()async{
      if(widget.permission == null){
        return;
      }
      var status = await widget.permission!.request();
      bool type = false;
      if(status.isGranted) {
        type = true;
      } else {
        type = false;
      }
      if (kDebugMode) {
        print("权限前 = ${ UserData.permissionValues.value}");
      }
      UserData.permissionValues.value[widget.permission!.value.toString()] = type;
      if (kDebugMode) {
        print("权限之后 =  ${UserData.permissionValues.value}");
      }
      StorageUtils().setPermission(UserData.permissionValues.value);
      Get.back(result: type);
    });
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return  Column(children: [
      Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          const Text("权限使用说明",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 18),),
          const SizedBox(height: 12,),
          Text(widget.desc ?? "无权限说明",style: TextStyle(fontSize: 14,color: AppColor.textColor),),
        ],).addContainer(width: double.infinity,padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 12),margin: const EdgeInsets.all(20))
    ],);
  }
}

